<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * kursus.php
 * @author ranuandra
 *
 * 10 Jul 2012 13:19:07
 *
 * 
 */

class Controller_Kursus extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$datas = $pelamar->pelamarpkursus->find_all();
		$view = View::factory('kursus/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_add()
	{
		$view = View::factory('kursus/form');
		$this->template->container = $view;
	}
	
	public function action_save()
	{
			if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			try 
			{
				
				if(isset($_POST['id']))
				{
					$pendidikan = ORM::factory('pelamarpendidikan', Request::current()->post('id'));
					$pendidikan->mstpendidikan_id = Request::current()->post('mstpendidikan_id');
					$pendidikan->mstjurusan_id = Request::current()->post('mstjurusan_id');
					$pendidikan->thn_lulus = Request::current()->post('thn_lulus');
					$pendidikan->type_pendidikan = Request::current()->post('type_pendidikan');
					$pendidikan->institusi = Request::current()->post('institusi');
					$pendidikan->save();
				}
				else 
				{
					$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
					$kursus = ORM::factory('pelamarkursus');
					$kursus->mstpelamar_id = $pelamar->id;
					$kursus->tgl_mulai =  $_POST['tahun_mulai']."-".$_POST['bulan_mulai']."-".$_POST['tanggal_mulai'];
					$kursus->tgl_selesai =  $_POST['tahun_selesai']."-".$_POST['bulan_selesai']."-".$_POST['tanggal_selesai'];
					$kursus->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
}